/*
 * StartApplication.java
 *
 * Created on October 8, 2006, 12:15 AM
 */

package org.sunspotworld;

import com.sun.spot.peripheral.Spot;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.io.j2me.socket.SocketConnection;
import com.sun.spot.util.*;


import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet {

    String mensaje;

    //Constantes
    private static final int PUERTO_BC_UNO = 67;
    private static final int PUERTO_BC_DOS = 68;
    
    //Variables de conexion
    RadiogramConnection rgConnSPOT;
    Datagram datagSpot;
    SocketConnection socketSpot;
    
    void ValoresIniciales(){
        mensaje = "usted esta en ucab";
        datagSpot = null;
    }
    
    private void enviarMensaje(){
        
        ITriColorLED [] led;
        led = EDemoBoard.getInstance().getLEDs();
        
        //Enviar mensajes a SPOT movil
        try{
            rgConnSPOT = (RadiogramConnection) Connector.open("radiogram://broadcast:" + PUERTO_BC_UNO);
            //rgConnSPOT = (RadiogramConnection) Connector.open("radiogram://broadcast:" + PUERTO_BC_DOS);
            datagSpot = rgConnSPOT.newDatagram(1000);
        }
        catch(Exception e){
            System.err.print(e.getMessage());
            notifyDestroyed();
        }
   
        while (true) {
            try {
                
                // Flash an LED to indicate a sampling event
                led[0].setRGB(0, 20, 0);
                led[0].setOn();
                /*Utils.sleep(1000);
                led[0].setOff();*/
                
                // Package the time and sensor reading into a radio datagram and send it.
                datagSpot.reset();
                datagSpot.writeChars(mensaje);
                rgConnSPOT.send(datagSpot);
            } catch (Exception e) {
                System.err.println("Caught " + e + " while collecting/sending sensor sample.");
            }
        }
    }
        
    protected void startApp() throws MIDletStateChangeException {
    
        ValoresIniciales();
        enviarMensaje();
        
        /*System.out.println("Hello, world");
        new BootloaderListener().start();   // monitor the USB (if connected) and recognize commands from host

        IEEEAddress ourAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress());
        System.out.println("Our radio address = " + ourAddr.asDottedHex());

        ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
        leds[0].setRGB(100,0,0);                // set color to moderate red
        while (true) {
            leds[0].setOn();                    // Blink LED
            Utils.sleep(250);                   // wait 1/4 seconds
            leds[0].setOff();
            Utils.sleep(3000);                  // wait 3 seconds
        }*/
    }

    protected void pauseApp() {
        // This will never be called by the Squawk VM
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // Only called if startApp throws any exception other than MIDletStateChangeException
    }
}
